package cn.wolfcode.wolf.user.advice;

import cn.wolfcode.wolf.core.exception.BusinessException;
import cn.wolfcode.wolf.core.utils.CodeMsg;
import cn.wolfcode.wolf.core.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class ControllerExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public Result<?> exceptionHandler(Exception e) {
        log.info("[统一异常处理]拦截到其他异常", e);
        return Result.defaultError();
    }
    @ExceptionHandler(BusinessException.class)
    public Result<?> businessExceptionHandler(BusinessException e) {
        if (log.isDebugEnabled()) {
            log.debug("[统一异常处理]拦截到业务异常", e);

        } else {
            log.warn("[统一异常处理]拦截到业务异常,code={},message={}", e.getCode(), e.getMessage());
        }
        return Result.error(new CodeMsg(e.getCode(), e.getMessage()));
    }


}
